<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ZixunController extends Controller
{
    //

	   public function getIndex()
    	{
             //查询资讯数据
            $data = DB::table('news')
                    ->orderBy('click','desc')
                    ->paginate(5); 


            return view('home/zixun/zixunzhu',['data'=>$data]);
    	}



        public function getPingrun(Request $request)
        {
            $id = $request->input('id');

          $news = DB::table('news')
            ->where('id',$id)
            ->first();

             //视频点击,点击量加1
            $click = $news->click + 1;
            DB::table('news')
            ->where('id',$id)
            ->update(['click'=>$click]);

            // ===============================
            //查询用户评论和对应用户头像姓名
            $ncomment = DB::table('ncomment')
                ->join('userdetail', 'ncomment.uid', '=', 'userdetail.uid')
                ->where('ncomment.nid',$id)
                ->orderBy('ncomment.id', 'desc')
                ->get();
            // ===============================

             return view('home/zixun/pingrun',['news'=>$news,'ncomment'=>$ncomment]);
            
        }

        public function getComment(Request $request)
        {
            $uid = session('uid');
            $nid = $request->input('nid');
            $comment = $request->input('comment');

              $res = DB::table('ncomment')
                    ->insertGetId(['uid' => $uid, 'nid' => $nid,'message' =>$comment,'time'=>time()]);

                $userdetail = DB::table('userdetail')
                    ->where('uid',$uid)
                    ->first();

            if($res){

                echo view('home.play.ping',['comment'=>$comment,'userdetail'=>$userdetail,'time'=>time()]);
            }else{
                echo 0;
            }

           }

        public function getFab(Request $request)
        {

            $fab = $request->input('fab')+1;
            $id = $request->input('id');
            $res = DB::table('ncomment')
                ->where('id',$id)
                ->update(['fabulous'=>$fab]);
                
            if ($res) {
                echo 1;
            }else{
                echo 0;
            }

        }


}
